package com.itmumu.crm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itmumu.crm.pojo.BaseDict;
import com.itmumu.crm.pojo.Customer;
import com.itmumu.crm.pojo.QueryVo;
import com.itmumu.crm.service.BaseDictService;
import com.itmumu.crm.service.CustomerService;
import com.itmumu.crm.utils.Page;
import com.mysql.cj.Query;
import com.sun.xml.internal.ws.api.model.wsdl.editable.EditableWSDLBoundFault;

/**
 * 
 * 客户信息请求处理
 * 
 * @author admin
 * @date 2020/01/21
 */
@Controller
@RequestMapping("customer")

public class CustomerController {

    @Autowired
    private BaseDictService dictService;

    @Autowired
    private CustomerService customerService;

    @Value("${customer_from_type}")
    private String customer_from_type;
    @Value("${customer_industry_type}")
    private String customer_industry_type;
    @Value("${customer_level_type}")
    private String customer_level_type;

    @RequestMapping("list")
    public String list(Model model, QueryVo vo) {
        // 查询来源
        List<BaseDict> fromType = dictService.queryBaseDictByCode(customer_from_type);
        // 查询行业
        List<BaseDict> industryType = dictService.queryBaseDictByCode(customer_industry_type);
        // 查询级别
        List<BaseDict> levelType = dictService.queryBaseDictByCode(customer_level_type);

        // 设置数据模型返回
        model.addAttribute("fromType", fromType);
        model.addAttribute("industryType", industryType);
        model.addAttribute("levelType", levelType);

        // 根据查询条件分页查询用户列表
        Page<Customer> page = customerService.getCustomerByQueryVo(vo);

        // 设置分页数返回
        model.addAttribute("page", page);

        // 返回查询条件
        model.addAttribute("vo", vo);

        return "customer";
    }

    @RequestMapping("edit")
    @ResponseBody
    public Customer edit(Integer id) {
        Customer customer = customerService.getCustomerById(id);
        return customer;
    }

    @RequestMapping("update")
    @ResponseBody
    public String update(Customer customer) {
        String msgString = "1";
        try {
            customerService.updateCustomer(customer);
            msgString = "0";
        } catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        }
        return msgString;
    }

    @RequestMapping("delete")
    @ResponseBody
    public String delete(Integer id) {
        String msg = "1";
        try {
            customerService.deleteCustomer(id);
            msg = "0";
        } catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        }
        return msg;
    }

    @RequestMapping("myTag")
    public String myTag() {
        return "myTag";
    }
}
